package xiangmu

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type ShiwuBody struct {
	ID         int64  `json:"id" gorm:"id"`
	Addtime    int64  `json:"addtime" gorm:"addtime"`
	Adminid    int64  `json:"adminid" gorm:"adminid"`
	Adminname  string `json:"adminname" gorm:"adminname"`
	Shixiang   string `json:"shixiang" gorm:"shixiang"`
	Tcr        string `json:"tcr" gorm:"tcr"`
	Tcrid      int64  `json:"tcrid" gorm:"tcrid"`
	Zrr        string `json:"zrr" gorm:"zrr"`
	Zrrid      int64  `json:"zrrid" gorm:"zrrid"`
	Wctime     int64  `json:"wctime" gorm:"wctime"`
	Status     int64  `json:"status" gorm:"status"` // 1未开始 2进行中 3已完成
	Gzzrr      string `json:"gzzrr" gorm:"gzzrr"`
	Gzzrrid    int64  `json:"gzzrrid" gorm:"gzzrrid"`
	Jinzhan    string `json:"jinzhan" gorm:"jinzhan"`
	Updatetime int64  `json:"updatetime" gorm:"updatetime"`
	Xiangmuid  int64  `json:"xiangmuid" gorm:"xiangmuid"`
	Laiyuan    string `json:"laiyuan" gorm:"laiyuan"`
}

func Shiwus(ctx *gin.Context) {
	request := ShiwuBody{}
	ctx.ShouldBind(&request)
	//fmt.Println(request)
	xiangmuid := request.Xiangmuid
	zrrid := request.Zrrid
	gzzrrid := request.Gzzrrid
	status := request.Status
	wctime := request.Wctime

	var mubiaos []models.Xiangmusw
	res := data_source.Db
	if xiangmuid > 0 {
		res = res.Where("xiangmuid = ?", xiangmuid)
	}
	if zrrid > 0 {
		res = res.Where("zrid = ?", zrrid)
	}
	if gzzrrid > 0 {
		res = res.Where("gzzrrid = ?", gzzrrid)
	}
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	if wctime > 0 {
		res = res.Where("wctime >= ? and wctime < ?", wctime, wctime+86400)
	}
	res = res.Order("status asc, id desc").Find(&mubiaos)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    mubiaos,
		"msg":     "ok",
	})
	return

}
func Shiwustatus(ctx *gin.Context) {
	request := ShiwuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	status := request.Status

	mb := models.Xiangmusw{
		Status: status,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Shiwuadd(ctx *gin.Context) {
	request := ShiwuBody{}
	ctx.ShouldBind(&request)
	xiangmuid := request.Xiangmuid
	adminid := request.Adminid
	shixiang := request.Shixiang
	tcr := request.Tcr
	tcrid := request.Tcrid
	zrr := request.Zrr
	zrid := request.Zrrid
	wctime := request.Wctime
	gzzrr := request.Gzzrr
	gzzrrid := request.Gzzrrid
	laiyuan := request.Laiyuan
	mb := models.Xiangmusw{
		Addtime:   time.Now().Unix(),
		Wctime:    wctime,
		Status:    1,
		Xiangmuid: xiangmuid,
		Adminid:   adminid,
		Shixiang:  shixiang,
		Tcr:       tcr,
		Tcrid:     tcrid,
		Zrr:       zrr,
		Zrrid:     zrid,
		Gzzrr:     gzzrr,
		Gzzrrid:   gzzrrid,
		Laiyuan:   laiyuan,
	}
	data_source.Db.Create(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Shiwuupdate(ctx *gin.Context) {
	request := ShiwuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	shixiang := request.Shixiang
	tcr := request.Tcr
	tcrid := request.Tcrid
	zrr := request.Zrr
	zrid := request.Zrrid
	wctime := request.Wctime
	gzzrr := request.Gzzrr
	gzzrrid := request.Gzzrrid
	laiyuan := request.Laiyuan
	mb := models.Xiangmusw{
		Wctime:   wctime,
		Shixiang: shixiang,
		Tcr:      tcr,
		Tcrid:    tcrid,
		Zrr:      zrr,
		Zrrid:    zrid,
		Gzzrr:    gzzrr,
		Gzzrrid:  gzzrrid,
		Laiyuan:  laiyuan,
	}
	data_source.Db.Where("id = ?", id).Updates(&mb)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Shiwudel(ctx *gin.Context) {
	request := ShiwuBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	adminname := request.Adminname
	adminid := request.Adminid
	var shiwu models.Xiangmusw
	data_source.Db.Where("id = ?", id).First(&shiwu)
	swdel := models.Xiangmuswdel{
		Addtime:     time.Now().Unix(),
		Adminid:     adminid,
		Adminname:   adminname,
		Xiangmuswid: id,
		Shixiang:    shiwu.Shixiang,
	}
	data_source.Db.Create(&swdel)
	data_source.Db.Where("id = ?", id).Delete(&models.Xiangmusw{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type ShiwudelsRBody struct {
	Data       []models.Xiangmuswdel `json:"data"`
	PageNo     int                   `json:"pageNo"`
	PageSize   int                   `json:"pageSize"`
	TotalCount int64                 `json:"totalCount"`
	TotalPage  int64                 `json:"totalPage"`
}

func Shiwudels(ctx *gin.Context) {
	request := XiangmusBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Xiangmuswdel
	res := data_source.Db
	res.Model(&models.Xiangmuswdel{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)
	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ShiwudelsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
